﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using DaInterFace;
using DaInterFace.SYN;
using DaInterFace.SYN.E01;

namespace E01
{
    /// <summary>
    /// 同步产品
    /// </summary>
    public class A03 : ISYNChildSet
    {
        public void Code(ISYN handler, CodeResult result, SYNRight user, SerialType st, String parameter)
        {
            E01A03P P = (E01A03P)handler.Serial.Deserialize(st, typeof(E01A03P), parameter);

            try
            {
                SqlParameter[] sps = new SqlParameter[7];
                sps[0] = new SqlParameter("@ID", SqlDbType.NVarChar, 255);
                sps[0].Value = P.ID;
                sps[1] = new SqlParameter("@Number", SqlDbType.NVarChar, 255);
                sps[1].Value = P.Number;
                sps[2] = new SqlParameter("@Name", SqlDbType.NVarChar, 255);
                sps[2].Value = P.Name;
                sps[3] = new SqlParameter("@IsBagged", SqlDbType.Bit);
                sps[3].Value = P.IsBagged;
                //sps[4] = newSqlParameter("@BagStandard", SqlDbType.NVarChar, 50);
                //sps[4].Value = P.BagStandard;
                //sps[5] = new SqlParameter("@CalPlan", SqlDbType.NVarChar, 50);
                //sps[5].Value = "";
                //sps[7] = new SqlParameter("@ConvertRatio", SqlDbType.Decimal);
                //sps[7].Value = P.ConvertRatio;
                sps[4] = new SqlParameter("@ProPrice", SqlDbType.Decimal);
                sps[4].Value = P.ProPrice;
                sps[5] = new SqlParameter("@TaxRate", SqlDbType.Decimal);
                sps[5].Value = P.TaxRate;
                sps[6] = new SqlParameter("@MaterialID", SqlDbType.VarChar, 255);
                sps[6].Value = P.MaterialID;

                StringBuilder sql = new StringBuilder();
                sql.Append("SET NOCOUNT ON\r\n");

                sql.Append("DECLARE @FID INT\r\n SET @FID=IsNull((SELECT top 1 FID FROM KEW_ProductsEntry WHERE FText=@ID),0)");
                sql.Append("Declare @material int select @material = isnull(FItemID,0) from t_ICItem where F_102 = @MaterialID \r\n");
                sql.Append("IF @FID is not null and @fid <> 0 BEGIN\r\n");
                sql.Append("  UPDATE KEW_Products SET FName=@Name WHERE FID=@FID\r\n");
                sql.Append("  UPDATE KEW_ProductsEntry SET FName=@Name,FCheckBox=@IsBagged,FDecimal2=@ProPrice,");
                sql.Append("  FDecimal3=@TaxRate,FText = @ID,FBase = @material WHERE  FID=@FID\r\n");
                sql.Append("  SELECT 'U',@FID\r\n");
                sql.Append("END \r\nELSE BEGIN\r\n");
                sql.Append("  EXEC GetICMaxNum 'KEW_Products',@FID OUTPUT\r\n");
                sql.Append("  INSERT INTO KEW_Products ([FID],[FNumber],[FName],[FParentID],[FLogic],[FDetail],[FDiscontinued],[FLevels],[FFullNumber],[FClassTypeID]) VALUES (@FID,@Number,@Name,0,-1,1,0,1,@Number,236900116)\r\n");
                sql.Append("  INSERT INTO KEW_ProductsEntry ([FClassTypeID],[FID],[FIndex],[FNumber],[FName],[FBase],[FBase4],[FCheckBox],[FBase1],[FBase3],[FBase6],[FBase2],[FDecimal],[FDecimal1],[FBase5],[FDecimal2],[FDecimal3],[FText]) ");
                sql.Append("  VALUES (236900116,@FID,0,@Number,@Name,@material,0,@IsBagged,0,0,0,0,0,1,0,@ProPrice,@TaxRate,@ID)\r\n");
                sql.Append("  SELECT 'I',@FID\r\n");
                sql.Append("END \r\n");
                sql.Append("SET NOCOUNT OFF\r\n");
                Recordset rs = handler.Data.ExeProcedure(P.BaseNo, sql.ToString(), sps);
                if (rs.Enable)
                {
                    if (rs.Data.Tables[0].Rows[0][0].ToString() == "U")
                    {
                        result.Result = EnumResult.Update;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    else
                    {
                        result.Result = EnumResult.Insert;
                        result.Content = rs.Data.Tables[0].Rows[0][1].ToString();
                    }
                    result.Message = String.Format("产品同步完成:{0},{1}", P.Number, result.Result.ToString());
                }
                else
                {
                    result.Result = EnumResult.None;
                    result.Message = String.Format("产品同步失败。{0}", "可能原因是:引用的基础资料未同步、信息录入错误或网络故障！");
                }
            }
            catch (Exception ex)
            {
                result.Result = EnumResult.None;
                result.Message = String.Format("产品同步失败:{0}", ex.Message);
            }
        }

    }

    /// <summary>
    /// 产品
    /// </summary>
    public class E01A03P
    {
        /// <summary>
        /// 账套编号
        /// </summary>
        public String BaseNo;
        /// <summary>
        /// 三方内码
        /// </summary>
        public String ID;
        /// <summary>
        /// 三方编号
        /// </summary>
        public String Number;
        /// <summary>
        /// 三方名称
        /// </summary>
        public String Name;
        /// <summary>
        /// 是否袋装
        /// </summary>
        public Boolean IsBagged;
        /// <summary>
        /// 袋装标准
        /// </summary>
        public String BagStandard;
        /// <summary>
        /// 计量方案
        /// </summary>
        public String CalPlan;
        /// <summary>
        /// 当前库存
        /// </summary>
        public Decimal CurrStock;
        /// <summary>
        /// 转换系数
        /// </summary>
        public Decimal ConvertRatio;
        /// <summary>
        /// 产品单价
        /// </summary>
        public Decimal ProPrice;
        /// <summary>
        /// 税率
        /// </summary>
        public Decimal TaxRate;
        /// <summary>
        /// EAS物料ID
        /// </summary>
        public String MaterialID;
        
    }
}
